package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.adapter.MyCollectTypeListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CompanyForumType;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.MyCollectType;
import com.isunland.managesystem.entity.MyCollectTypeOriginal;
import com.isunland.managesystem.utils.SharedPreferencesUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:  我收藏的分类列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/3/16 17:15
 */
public class MyCollectTypelistFragment extends BaseListFragment {
  private ArrayList<MyCollectType> mList;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    mList = new ArrayList<>();
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_COLLECT_TYPE;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("myjobno", CurrentUser.newInstance(getActivity()).getJobNumber());
    /** 01: 农技服务,02: 农机服务*/
    params.put("topicKindCode",
        SharedPreferencesUtil.getString(mActivity, SharedPreferencesUtil.KEY_FORUM_SUBCODE, ""));
    return params;
  }

  @Override public void resolveResponse(String json) {

    MyCollectTypeOriginal original = new Gson().fromJson(json, MyCollectTypeOriginal.class);
    if (original.getResult() == Base.SUCCESS && original.getRows() != null) {
      //获取列表成功
      mList.clear();
      mList.addAll(original.getRows());
      MyCollectTypeListAdapter mAdapter = new MyCollectTypeListAdapter(getActivity(), mList);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    //跳转到二级大豆,玉米详情列表
    CompanyForumType item = new CompanyForumType();
    item.setName(mList.get(position - 1).getCollecttypename());
    item.setCustomAttrs(mList.get(position - 1).getCollecttypecode());
    Intent intent = new Intent(getActivity(), AgriculturalForumListActivity.class);
    intent.putExtra(AgriculturalForumListFragment.EXTRA_VALUE, item);
    intent.putExtra(AgriculturalForumListFragment.EXTRA_FROM,
        AgriculturalForumListFragment.COLLECT_TYPE);
    startActivityForResult(intent, 0);
    super.onListItemClick(l, v, position, id);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    volleyPost();
    super.onActivityResult(requestCode, resultCode, data);
  }
}